/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package org.groupg;

import java.io.*;
import java.time.LocalDate;
import java.util.*;

import org.apache.commons.collections4.ListUtils;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.csv.CSVRecord;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.tika.Tika;
import org.apache.tika.exception.TikaException;
import org.groupg.model.CustInfo;
import org.groupg.model.OrgInfo;
import org.groupg.model.UserInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.sql.DataSource;

/**
 * @author ligen
 */
public class DataInit {

    public static Logger log = LoggerFactory.getLogger(DataInit.class);

    public static List<OrgInfo> orgInfos = new ArrayList<OrgInfo>();
    public static List<UserInfo> userInfos = new ArrayList<UserInfo>();
    public static List<CustInfo> custInfos = new ArrayList<CustInfo>();
    public static List<String> sqlList = new ArrayList<String>();


    public static void main(String[] args) throws IOException {


        try {
            sqlList = IOUtils.readLines(Objects.requireNonNull(DataInit.class.getResourceAsStream("/init.sql")), "UTF-8");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        IOUtils.close();
        if (sqlList == null) sqlList = new ArrayList<String>();
        Reader orginfoIn = new InputStreamReader(Objects.requireNonNull(DataInit.class.getResourceAsStream("/publicData-orginfo.csv")));
        Reader userinfoIn = new InputStreamReader(Objects.requireNonNull(DataInit.class.getResourceAsStream("/publicData-userinfo.csv")));
        // 读取机构信息表
        Iterable<CSVRecord> recordsOrginfo = null;
        try {
            recordsOrginfo = CSVFormat.RFC4180.withHeader("orgid", "orgname", "orgtype").parse(orginfoIn);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        for (CSVRecord record : recordsOrginfo) {
//            String lastName = record.get("Last Name");
//            String firstName = record.get("First Name");
            String orgid = record.get("orgid");
            String orgname = record.get("orgname");
            String orgtype = record.get("orgtype");

            orgInfos.add(new OrgInfo(orgid, orgname, orgtype)); // 初始化 orginfos 列表
        }
        orgInfos.remove(0);
        // 读取用户信息数据
        Iterable<CSVRecord> recordsUserinfo = null;
        try {
            recordsUserinfo = CSVFormat.RFC4180.withHeader("userid", "username", "orgid", "usertype", "userstatue").parse(userinfoIn);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        for (CSVRecord record : recordsUserinfo) {
//            String lastName = record.get("Last Name");
//            String firstName = record.get("First Name");

            String userid = record.get("userid"), username = record.get("username"), orgid = record.get("orgid"), usertype = record.get("usertype"), userstatue = record.get("userstatue");
            userInfos.add(new UserInfo(userid, username, orgid, usertype, userstatue)); // 初始化 userinfos 列表
        }
        userInfos.remove(0);

        orginfoIn.close();
        userinfoIn.close();


/*
        custid	custname	custtype	userid	custstatue
        CI202211050000001	客户0000001	1	UI010001	1
*/

        int idnum = 1000001;

        Collections.shuffle(userInfos);// 对用户数据重排
        for (UserInfo user:userInfos) {
            double d = Math.random()*1000;
            for (int i = 0; i < d; i++) {
                String custid = "CI20221105"+idnum;
                String custname = "客户"+idnum;
                String userid = user.getUserid();
                // 如果随机数能被6整除则客户认为是企业客户
                String custtype = i%6==0?"2":"1";
                String custstatue = "1";
                if(i%39==0){
                    // 违约客户 每100个里面有两个
                    custstatue = "3";
                }else if(i%20==0){
                    // 优质客户 每100个 有5个 其他的是一般客户
                    custstatue = "2";
                }
                CustInfo custinfo = new CustInfo(custid,custname,custtype,userid,custstatue);
                custInfos.add(custinfo);
                sqlList.add(custinfo.getInsertSQLString());
                idnum = idnum + 1;
            }
        }
        log.debug("sqlList:{} ,orgInfos:{},userInfos:{},custInfos:{}", sqlList.size(), orgInfos.size(), userInfos.size(),custInfos.size());
        FileUtils.writeLines(new File("target/outFile/insert.sql"),sqlList);

        // 把 custInfos CSV 输出到文件中
        try (CSVPrinter printer = new CSVPrinter(new FileWriter("target/outFile/custData.csv"), CSVFormat.EXCEL)) {
            printer.printRecord("custid","custname","custtype","userid","custstatue");
            for (CustInfo custInfo : custInfos) {
                printer.printRecord(custInfo.getCustid(), custInfo.getCustname(), custInfo.getCusttype(), custInfo.getUserid(), custInfo.getCuststatue());
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

}
